package com.ruoyi.web.controller.pk;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pk.domain.PkZlInstruction;
import com.ruoyi.pk.service.IPkZlInstructionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 主粮指导书Controller
 *
 * @author wang
 * @date 2021-09-10
 */
@RestController
@RequestMapping("/pk/zl_instruction")
public class PkZlInstructionController extends BaseController
{
    @Autowired
    private IPkZlInstructionService pkZlInstructionService;

    /**
     * 查询主粮指导书列表
     */
    @PreAuthorize("@ss.hasPermi('pk:zl_instruction:list')")
    @GetMapping("/list")
    public TableDataInfo list(PkZlInstruction pkZlInstruction)
    {
        startPage();
        List<PkZlInstruction> list = pkZlInstructionService.selectPkZlInstructionList(pkZlInstruction);
        return getDataTable(list);
    }

    /**
     * 导出主粮指导书列表
     */
    @PreAuthorize("@ss.hasPermi('pk:zl_instruction:export')")
    @Log(title = "主粮指导书", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PkZlInstruction pkZlInstruction)
    {
        List<PkZlInstruction> list = pkZlInstructionService.selectPkZlInstructionList(pkZlInstruction);
        ExcelUtil<PkZlInstruction> util = new ExcelUtil<PkZlInstruction>(PkZlInstruction.class);
        return util.exportExcel(list, "主粮指导书数据");
    }

    /**
     * 获取主粮指导书详细信息
     */
    @PreAuthorize("@ss.hasPermi('pk:zl_instruction:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pkZlInstructionService.selectPkZlInstructionById(id));
    }

    /**
     * 新增主粮指导书
     */
    @PreAuthorize("@ss.hasPermi('pk:zl_instruction:add')")
    @Log(title = "主粮指导书", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PkZlInstruction pkZlInstruction)
    {
        return toAjax(pkZlInstructionService.insertPkZlInstruction(pkZlInstruction));
    }

    /**
     * 修改主粮指导书
     */
    @PreAuthorize("@ss.hasPermi('pk:zl_instruction:edit')")
    @Log(title = "主粮指导书", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PkZlInstruction pkZlInstruction)
    {
        return toAjax(pkZlInstructionService.updatePkZlInstruction(pkZlInstruction));
    }

    /**
     * 删除主粮指导书
     */
    @PreAuthorize("@ss.hasPermi('pk:zl_instruction:remove')")
    @Log(title = "主粮指导书", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pkZlInstructionService.deletePkZlInstructionByIds(ids));
    }

    /**
     * 根据条件获取数据集合
     */
    @GetMapping("/getDataByCon")
    public List<String> getDataByCon(PkZlInstruction pkZlInstruction)
    {
        List<PkZlInstruction> list = pkZlInstructionService.selectPkZlInstructionList(null);
        List<String> data = new ArrayList<>();

        if(null!=pkZlInstruction.getHj()) {
            data = list.stream().filter(item ->
                    item.getHj().equals(pkZlInstruction.getHj())).
                    map(PkZlInstruction::getWtlb).distinct().collect(Collectors.toList());
        }
        if(null!=pkZlInstruction.getWtlb()){
            data = list.stream().filter(item ->
                    item.getHj().equals(pkZlInstruction.getHj()) &&
                    item.getWtlb().equals(pkZlInstruction.getWtlb())).
                    map(PkZlInstruction::getWtdj).distinct().collect(Collectors.toList());
        }
        if(null!=pkZlInstruction.getWtdj()){
            data = list.stream().filter(item ->
                    item.getHj().equals(pkZlInstruction.getHj()) &&
                    item.getWtlb().equals(pkZlInstruction.getWtlb()) &&
                    item.getWtdj().equals(pkZlInstruction.getWtdj())).
                    map(PkZlInstruction::getScbcfje).distinct().collect(Collectors.toList());
        }
        return data;
    }

    /**
     * 获取指导书条项
     */
    @GetMapping("/getZdstxList")
    public List<PkZlInstruction> getZdstxList(PkZlInstruction pkZlInstruction)
    {
        List<PkZlInstruction> list = pkZlInstructionService.selectPkZlInstructionList(null).stream().map(m->{
            PkZlInstruction zl = new PkZlInstruction();
            zl.setZdstx(m.getZdstx());
            zl.setYq(m.getYq());
            return zl;
        }).collect(Collectors.toList());
        return list;
    }
}
